{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5de9ea47",
   "metadata": {},
   "source": [
    "介绍 query doc 语义相关性\n",
    "\n",
    "![语义相似度](./images/qu_rel.png)\n",
    "Query和Doc经过一个表示层，将输入转换为2个向量，计算这两个向量的cos距离，再过一个softmax，使用交叉熵作为损失函数。\n",
    "考虑一个query下召回的doc的相对相似度，作为一个排序问题，可以借鉴前面提到的point-wise,paire-wise,list-wise的思想。\n",
    "\n",
    "https://github.com/JepsonWong/Text_Matching\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fdcddebf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.2423,  1.2422],\n",
      "        [-1.1246,  1.2047],\n",
      "        [ 0.7806, -0.2597]])\n",
      "tensor([[-0.8846, -0.2148],\n",
      "        [-0.2815,  0.3075],\n",
      "        [ 0.6451,  0.8955]])\n",
      "tensor([-0.0455,  1.0000,  0.2985])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "\n",
    "# 两个向量的相似度度量\n",
    "# 1.余弦相似度，两个特征在经过L2归一化之后的矩阵内积。取值范围[-1,1]，要计算的连个向量维度必须一致\n",
    "# https://pytorch.org/docs/master/generated/torch.nn.functional.cosine_similarity.html#torch.nn.functional.cosine_similarity\n",
    "input1 = torch.randn(3, 2)\n",
    "print(input1)\n",
    "input2 = torch.randn(3, 2)\n",
    "print(input2)\n",
    "output = F.cosine_similarity(input1, input2)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bdd2086",
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://github.com/nishnik/Deep-Semantic-Similarity-Model-PyTorch/blob/master/cdssm.py\n",
    "# "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
